package com.ldu.exam.controller;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ldu.exam.model.MenuModel;
import com.ldu.exam.model.RoleModel;
import com.ldu.exam.service.RoleService;


@RequestMapping("/roles")
@RestController
public class RoleController 
{
	@Autowired private  RoleService service ;
	/**
	 * 查询全部角色信息(分页)
	 * @param page
	 * @return
	 */
	@GetMapping(params="pageable")
	public Page<RoleModel> findAll( @PageableDefault(size=20) Pageable page)
	{
		return this.service.findAll( page) ;
	}
	
	@GetMapping()
	public Iterable<RoleModel> findByUserId(@RequestParam String userId)
	{
		return this.service.findAllByUserId( userId) ;
	}
	
	@GetMapping(value="/noPage")
	public Object findAllByNoPage()
	{
		return this.service.findByNoPage();
	}
	/**
	 * 根据ID查询角色信息
	 * @param id 主键ID
	 * @return 角色信息
	 */
	@GetMapping("/{id}")
	public RoleModel find( @PathVariable String id)
	{
		return this.service.find(id);
	}
	
	/**
	 * 删除角色
	 * @param id
	 */
	@DeleteMapping("/{id}")
	public Object delete( @PathVariable String id )
	{
		this.service.delete(id);
		return ResponseEntity.noContent().build();
	}
	
	/**
	 * 根据名称查询角色
	 * @param name
	 * @param page
	 * @return
	 */
	@GetMapping(params="name")
	public  Page<RoleModel> findByName( @RequestParam String name, 
		 								 @PageableDefault(size=20) Pageable page)
	{
		return this.service.findByName( name, page) ;
	}
	
	/**
	 * 修改角色信息
	 * @param id
	 * @param model
	 * @return
	 */
	@PutMapping("/{id}")
	public RoleModel put( @PathVariable String id,@RequestBody RoleModel model)
	{
		return this.service.update( id, model);
	}
	
	/**
	 * 创建角色
	 * @param model
	 * @return
	 */
	@PostMapping()
	public RoleModel save(@RequestBody RoleModel model)
	{
		return this.service.save(model);
	}
	
//	/**
//	 * 角色分配权限
//	 * @param id
//	 * @param resources
//	 * @return
//	 */
//	@PutMapping("/{id}/resources")
//	public Object  putResources( @PathVariable String id , @RequestBody Set<ResourceModel> resources)
//	{
//		return this.service.putResources(id ,resources) ;
//	}
	
	@GetMapping("/confirmRole")
	public Object confirmRole(@RequestParam String roleId)
	{
		return this.service.isStudent(roleId);
	}
	/**
	 * 角色分配菜单
	 * @param id
	 * @param menus
	 * @return
	 */
	@PutMapping("/{id}/menus")
	public Object putMenus( @PathVariable String id , @RequestBody Set<MenuModel> menus)
	{
		return this.service.putMenus(id , menus) ;
	}
	
	/**
	 * 判断名字是否重复
	 * @param name
	 * @return
	 */
	@GetMapping("/isExit")
	public Object isExit(@RequestParam String name) 
	{
		return this.service.isExit(name);
	}
	
}
